﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;
using VitCode.WorkFlow.DataAccess;
using VitCode.WorkFlow.Entities;

namespace WorkFlowSettingApp
{
    public partial class ucWorkItem : UserControl
    {

        private const int EACH_WIDTH = 60;

        private WorkFlowAccess m_Access = new WorkFlowAccess();

        public string OrderString { get; set; }

        public string CreateOrderString(string parentOrderString,int levelNum)
        {
            string strHead = "";
            if (!string.IsNullOrEmpty(parentOrderString))
                strHead = parentOrderString + ".";
            return  strHead+(900000000 + 10000*levelNum + App_FlowId);
        }

        public ucWorkItem()
        {
            InitializeComponent();
            App_FlowId++;
            FlowId = App_FlowId;
        }

        public Color BgColor
        {
            get { return txt.BackColor; }
            set
            {
                cmPop.Enabled = (value == SettingHelper.COLOR_SELECTED);
                txt.BackColor = value;
            }
        }

        public new string Text
        {
            get { return txt.Text; }
            set { txt.Text = value; }

        }

        public static int App_FlowId
        {
            get;
            set;
        }

        public int LevelNum { get; set; }

        public int FlowId { get; set; }

        public string ParentCtlNames { get; set; }

        public WorkItem WorkItem { get; set; }

        public string ChildCtlNames { get; set; }

        public string LineCtlNames { get; set; }

        private Color m_ItemColor;

        public Color ItemColor
        {
            get { return m_ItemColor; }
            set
            {
                m_ItemColor = value;
                if (txt.BackColor == Color.FromName("Window"))
                    txt.BackColor = m_ItemColor;
            }
        }

        private void txt_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left && WorkItem.Flag != WorkItem.FLAG_END)
            {
                if (SettingHelper.IsKeyDownCtrl)
                {
                    if (BgColor == SettingHelper.COLOR_SELECTED)
                    {
                        if (SettingHelper.SelectCtlNames.IndexOf(Name) > -1)
                            SettingHelper.SelectCtlNames =
                                        SettingHelper.SelectCtlNames.Remove(SettingHelper.SelectCtlNames.IndexOf(Name),
                                                                            Name.Length + 1);
                    }
                    else 
                    {
                        SettingHelper.SelectCtlNames = SettingHelper.SelectCtlNames + Name + ",";
                    }
                }
                else 
                {
                   SettingHelper.SelectCtlNames = Name + ",";
                }
                string selectedCtls = SettingHelper.SelectCtlNames;
                foreach(Control c in Parent.Controls)
                {
                    if(c is ucWorkItem)
                    {
                        var itemCtl = c as ucWorkItem;
                        itemCtl.BgColor = selectedCtls.IndexOf(c.Name) > -1 ? SettingHelper.COLOR_SELECTED : itemCtl.ItemColor;
                    }
                }
            }
        }

        private void txt_DoubleClick(object sender, EventArgs e)
        {
            WorkItemEdit wie = new WorkItemEdit();
            wie.KeyId = WorkItem.Id;
            wie.ShowDialog();
        }

        private void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue==17)
                SettingHelper.IsKeyDownCtrl = true;
        }

        private void txt_KeyUp(object sender, KeyEventArgs e)
        {
            SettingHelper.IsKeyDownCtrl = false;
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var obj = FindForm();
            if(obj != null && obj is ActivityCreator)
            {
                var frm = ((ActivityCreator) obj);
                frm.CreateWorkItem();
                //SettingHelper.SelectCtlNames = "";
            }
        }

        private void relationToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void smiEnd_Click(object sender, EventArgs e)
        {
            WorkFlowAccess acc = new WorkFlowAccess();
            acc.BeginTransaction();
            try
            {
                WorkItem.Flag = WorkItem.FLAG_END;
                WorkItem.Name = "结束";
                WorkItem.FixType = WorkItem.FIX_NEED;
                txt.Text = WorkItem.Name;
                ItemColor = SettingHelper.COLOR_END;
                BgColor = ItemColor;
                acc.SaveOrUpdate(WorkItem);
                acc.Commit();
            }
            catch(Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
            
        }
    }
}
